<?php 
include('../Connections/KRAPPERCON.php');
$can_guia = 0;
$can_print = 0;
$oper = '';
$fneto = 0;
$ftotal = 0;
$fiva = 0;
$guia_ok = 0;
$guia = '';
$cond_vta = '';
$pedcli = '';
$codcli = '';
$nomcli= '';
$find = false;
$fcladoc = '';
$dircli= '';
$found = false;
$factura = '';
$fecdoc= '';
$condvta = '';
$userid = '';
$feccre= '';
$status = '';
$folio = '';
$tipCam = 0.000;
$centroFac = '';
$ot = 0;
$moneyID = '';

$combo = '';
include('../Query_Min/getAlmacenes.php');

if (isset($_GET['oper'])){
$oper = $_GET['oper'];
}

if (isset($_POST['oper'])){
$oper = $_POST['oper'];
}

if ($oper == 'view'){
$factura = getGet('t_fc_nrodoc');

if ($factura == ''){
$factura = getVal('t_fc_nrodoc');
}

if (trim($factura) != '' ) {

$sql  = "call sp_getFacturaCab('".$factura."')";

$res1 = $mysqli->query($sql) or die($mysqli->error.__LINE__);

	if ($res1){
	// Muestra los Datos de cabecera
	if ($row = $res1->fetch_assoc()){
	
	$found = true;
		$codcli = $row['ClientID'];
		$ot = $row['OrdenTraID'];
		$nomcli = $row['ClientNom']." ".$row['ClientApePat']." ".$row['ClientApeMat'];
		$dircli = $row['ClientDirec'];	
		$pedcli = $row['PedidoID'];
		$fcladoc = $row['ClaDocID'];
		$fecdoc = $row['FactFecFac'];
		$condvta = $row['CondVenID'];
		$centroFac = $row['CentroID'];
		$userid = $row['UserID'];
		$feccre = $row['FactFecCre'];
		$folio = $row['FactFolio'];	
		$fsts = $row['FactAnul'];
		$guia = $row['FactGuia'];
		$kard = $row['FactKard'];
		$texto = $row['FactTxt'];
		$moneyID = $row['MoneyID'];
		$fneto = $row['FactNet'];
		$fiva = $row['FactIva'];
		$ftotal = $fneto + $fiva;
		$can_guia = $row['ClaDocGui'];
		$tipCam = $row['TipCam'];
		
		if ($fsts == 1 ){
			$can_print = 1;
			$status = '[Anulado]';
		}
		elseif($fsts == 2){
			$status = '[Anulacion]';
		}
		elseif ($fsts == 0){
			$guia_ok = 1;
			$can_print = 1;			
			$status = '[Activo]';
		}
	}
	$res1->close();
	$mysqli->check_result();
	}
	}
}
?>

<div>
<?php 
$css = '';
if (trim($folio)=='') {
$css = 'display:none';
}
?>
<input name="input" style="<?php //echo $css; ?>" type="button" id="btn_vf_prnt" value="Imprimir" onclick="printFactura()" />&nbsp;

<input name="input" type="button" value="Anular Documento" onclick="anularFactura()" />&nbsp;
  <input name="input" type="button" value="Asignar Folio" onclick="asignarFolio()" />
&nbsp;&nbsp;
<input type="text" name="t_vf_nrodoc" id="t_vf_nrodoc" onkeypress="enterFact(event)" />
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tbl_fc_dat">

    <?php 
	if ($found){
	?>
  <tr>
    <td colspan="4">
    <div class="subTit">
	<?php 
    echo "Documento ".$factura." creador por ".$userid." el ".$feccre." ".$status;
    ?>
    </div>    </td>
  </tr>
  <?php 
	
	}
	?>
  <tr>
    <td>Clase Documento :</td>
    <td><span id="sp_nrofac">
      <select name="cb_fc_cldoc" id="cb_fc_cldoc" disabled="disabled">
      <?php 
       $cladoc = 'FV';
	 	include ('../Query_Min/getClaDocs.php');
      ?>
      </select>
    </span></td>
    <td>Nro Documento :</td>
    <td><input name="t_fc_nrodoc" type="text" id="t_fc_nrodoc" size="12" maxlength="10" disabled="disabled"  value="<?php echo $factura; ?>"  /></td>
  </tr>
  <tr>
    <td>Fecha Documento :</td>
    <td><input name="t_fc_fecdoc" type="text"  value="<?php echo $fecdoc; ?>"  id="t_fc_fecdoc" size="14" maxlength="10" disabled="disabled" /></td>
    <td>Condicion :</td>
    <td><select name="cb_fc_condv" id="cb_fc_condv" disabled="disabled">
    <?php 
         
	 include ('../Query_Min/getCondVenta.php');
      ?>
    </select></td>
  </tr>
  <tr>
    <td>Cliente :</td>
    <td colspan="3">
      <input name="t_fc_codcli" type="text" id="t_fc_codcli" disabled="disabled"  value="<?php echo $codcli; ?>"  size="12" maxlength="10" />
      <input type="text" name="t_fc_nomcli" id="t_fc_nomcli"  value="<?php echo $nomcli; ?>"  disabled="disabled" />    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="3">
      <input name="t_fc_dircli" type="text" id="t_fc_dircli" size="50" maxlength="50"  disabled="disabled" value="<?php echo $dircli; ?>" />
    </td>
  </tr>
  <tr>
    <td>Asignacion :</td>
    <td><input style="font-weight:bold;" name="t_fc_nrofol" type="text" id="t_fc_nrofol" disabled="disabled" size="30" maxlength="20" value="<?php echo $folio; ?>" /></td>
    <td><?php 
	if ($ot==0) { echo "Pedido :"; }
	if ($ot > 0) { echo "OT :"; } ?></td>
    <td>
    <?php 
	if ($ot>0) { ?>
     <a href="#page/Mantenimientos/OrdTrabajo/adicionar.php?opcion=1&oper=view&idOrdTra=<?php echo $ot;?>"><?php echo $ot; ?></a>  
     <?php } ?>
      <?php 
	if ($ot==0) { ?>
     <a href="#page/Procesos/MantPedidos.php?oper=view&pedido=<?php echo $pedcli;?>"><?php echo $pedcli; ?></a>  
     <?php } ?>       </td>
  </tr>
    <tr>
      <td>Texto :</td>
      <td><textarea name="t_fc_text" cols="60" id="t_fc_text" disabled="disabled"></textarea></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <?php 
	if ($can_guia ==1 && $guia_ok == 1) { 
	// Si el Doc. puee tener guia y esta activo, es posible generar guia o mostrar si la tiene
	?>
    <tr>
    <td>Guia de Remision :</td>
    <?php
	$d_guia = '';
	if (trim($guia)==''){
	$d_guia = 'Generar Guia';
	}
	else {
	$d_guia = $guia;
	}
	 ?>
    <td><input type="button" id="btn_do_guia" onclick="generarGuia('<?php echo $centroFac;?>')" title="<?php echo $guia;?>" value="<?php echo $d_guia; ?>" /><span id="sp_no_guia"></span></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <?php 
  } 
  ?>
  <tr class="onlyInput">
    <td>Doc.Pago :</td>
    <td>
      <input type="button" name="btn_verdp" id="btn_verdp" onclick="verDocPagFV()" value="Documentos de Pago" />
    </td>
    <td>Moneda&nbsp;/&nbsp;Tip.Cambio :</td>
    <td><input name="t_moneFac"  type="text" id="t_moneFac" class="disabled" size="10" maxlength="4" disabled="disabled" />
      &nbsp;/&nbsp;
      <input name="t_tipcam" type="text" id="t_tipcam" class="disabled qt" disabled="disabled" size="10" maxlength="6"/>
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="4"><table style="margin-bottom:5px;" width="100%" border="1" rules="all" cellspacing="0" cellpadding="0">
    <thead>
  <tr>
    <th>Almacen</th>
    <th>Articulo</th>
    <th>Descripcion</th>
    <th>P.Unitario</th>
    <th>Cantidad</th>
    <th>Dscto(%)</th>
    <th>Costes</th>
    <th>Neto</th>
    <th>IGV</th>
  </tr>
  </thead>
  <tbody>
  <?php 
  $subt = 0;
  $subt_igv = 0;
  $tot = 0;
  if (trim($factura) != '')  {
  $sql  = "call sp_getFacturaDet('".$factura."')";
  $res2 = $mysqli->query($sql) or die($mysqli->error.__LINE__);
  if ($res2){
  	while($row = $res2->fetch_assoc()){
	$subt += $row['DetFactNet'];
	$subt_igv += $row['DetFactIva'];
	$alm = $row['AlmacenID'];
  ?>
  <tr>
   <td><select disabled="disabled"><?php echo str_replace("value='".$alm."'","value='".$alm."' selected='selected'",$combo);   ?></select></td>
    <td><?php echo $row['ArticuloID'];?></td>
   <td><?php echo $row['DetFactTxt'];?></td>
    <td class="qt"><?php echo $row['DetFactPru'];?></td>
    <td class="qt"><?php echo $row['DetFacQty'];?></td>
   <td class="qt"><?php echo $row['DetFactPdt'];?></td>
   <td class="qt"><?php echo $row['DetFactCst'];?></td>
    <td class="qt"><?php echo $row['DetFactNet'];?></td>
    <td class="qt"><?php echo $row['DetFactIva'];?></td>
  </tr>

  <?php 
  	}
	$tot = $subt + $subt_igv;
	$res2->close();
	$mysqli->check_result();
  }
  }
  ?>
  </tbody>
  <tfoot>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  </tfoot>
</table></td>
  </tr>
  <tfoot>
   <tr>
     <td>SubTotal :</td>
     <td>
       <input name="t_fc_subt" class="qt" disabled="disabled"  type="text" id="t_fc_subt" size="15" align="right" />    </td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
   </tr>
   <tr>
     <td>IGV</td>
     <td>
       <input name="t_fc_igv" class="qt" disabled="disabled" type="text" id="t_fc_igv" size="15"  align="right"/>     </td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
   </tr>
   <tr>
    <td>Total :</td>
    <td>
      <input name="t_fc_total" class="qt" disabled="disabled"  type="text" id="t_fc_total" size="15" align="right" />  </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
   </tr>
  </tfoot>
</table>

<script type="text/javascript">
//showOk('<?php echo $factura;?>');
<?php  
if ($found){
?>
GEBI('cb_fc_condv').value = '<?php echo $condvta;?>';
GEBI('cb_fc_cldoc').value = '<?php echo $fcladoc;?>';
GEBI('t_fc_text').value = '<?php echo $texto; ?>';
GEBI('t_fc_subt').value = myRound('<?php echo $fneto;?>');
GEBI('t_fc_igv').value = myRound('<?php echo $fiva;?>');
GEBI('t_fc_total').value = myRound('<?php echo $ftotal;?>');
GEBI('t_moneFac').value = '<?php echo $moneyID; ?>';
GEBI('t_tipcam').value = '<?php echo $tipCam;?>';
<?php 
}
else if (trim($factura)!= '') {
?>
showOk('No se ha encontrado el Dcto.Factura <?php echo $factura ;?>');
<?php 
}
if ($can_print == 0){
?>
$('#btn_vf_prnt').remove();
<?php
}
?>

function printFactura(){
	var fact = GEBI('t_fc_nrodoc').value;	
	var tipo = GEBI('cb_fc_cldoc').value;
	tipo = tipo.substr(3,2);
	if (tipo == 'N1' || tipo == 'N2' || tipo == 'N3'){
	window.open('Prints/printNC.php?oper=print&factura='+fact);
	}
	else if (tipo == 'FC') {
	window.open('Prints/printFacturaTxts.php?oper=print&factura='+fact);
	}
	
	else if (tipo == 'BL'){
	window.open('Prints/printBoleta.php?oper=print&factura='+fact);
	}
}

function asignarFolio(){
	sendData('Procesos/Opers/OperFact.php?oper=folio&factura='+GEBI('t_fc_nrodoc').value);
}

function enterFact(e){
	if (e.keyCode == 13){
		loadPage('Procesos/verFactura.php?oper=view&t_fc_nrodoc='+ GEBI('t_vf_nrodoc').value);
	}
}

function anularFactura(){
	var doc = GEBI('t_fc_nrodoc').value;
	if ($.trim(doc)==''){
	showOk('No se ha cargado ningun documento');
	return;
	}

	if (confirm('Confirme para anular Documento '+doc) ) {
		sendData('Procesos/Opers/OperFact.php',{oper : 'anular' , factura : doc});
	}
}

function generarGuia(centro){
	var doc = GEBI('t_fc_nrodoc').value;
	var guia = $.trim($('#btn_do_guia').attr('title'));
	var cli = GEBI('t_fc_codcli').value;
	if (guia == ''){	
	// sendData('Procesos/Opers/OperFact.php',{oper : 'guia' , factura : doc});
	loadPopup({url : 'Procesos/doGuia.php', params : {return : "doGuiaFac('"+doc+"')", doc : doc , factura : 'X' , cliente : cli , partida : centro } });
	}
	else {
	window.open('Prints/printGuiaRem.php?oper=print&clase=FAC&factura='+doc+'&guia='+guia);
	}
}

$('#tbl_fc_dat').disable();

function doGuiaFac(num){
	if (!$("#tbl_frm_gfa").checkInput()){
	return;
	}
	var doc = GEBI('t_fc_nrodoc').value;
	doc = num ;
	sendData('Procesos/Opers/OperFact.php', 
		{ 
		oper : 'guia' ,
		factura : doc,
		ClientID : GEBI('t_gu_cdc').value,
		ClientNIF : GEBI('t_gu_nif').value,
		ClientNom : GEBI('t_gu_raz').value,
		ClientDir : GEBI('t_gu_dir').value,
		Destino : GEBI('t_gu_cdc2').value,
		Partida : GEBI('cbo_pto_exp').value,
		texto : GEBI('t_gu_txt').value
		} 
	);
}

function verDocPagFV(){
	var doc = GEBI('t_fc_nrodoc').value;
	loadPopup({ url : 'Consultas/ListDocPago.php', params : { oper : 'byFact', doc : doc } });
}
</script>

